package org.adream.account.rest.frontApi;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.adream.account.model.LoginModel;
import org.adream.account.model.ResultModel;
import org.adream.account.service.LoginService;
import org.adream.account.util.UserUtil;
import org.adream.account.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 登录  前端
 * @author sl
 *
 */
@RequestMapping(value = "/loginFront/")
@Controller
public class LoginFrontApiController {
	
	@Autowired
	private LoginService loginService;
	
	// 公众号平台appid
	@Value("${wx.app.appid}")
	private String wxAppAppid;
	
	// 微信开放平台appid
	@Value("${wx.appid}")
	private String wxAppid;
	
	private final static Logger logger = LoggerFactory.getLogger(LoginFrontApiController.class); 
	
	/**
	 * 根据登录方式更新login, 用户自己更改密码
	 * @param loginModel
	 * @return
	 */
	@RequestMapping(value = "updateLogin", produces = "application/json;charset=UTF-8",method = RequestMethod.POST)
	@ResponseBody
	public ResultModel<?> String(@RequestBody LoginModel loginModel, HttpServletRequest request) {
		loginModel.setUid((loginModel.getUid() == null ? UserUtil.getUidByRequest(request) : loginModel.getUid()));
		if(StringUtils.isEmpty(loginModel) || StringUtils.isEmpty(loginModel.getUid())) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "更新失败,请求参数有误", null);
		}
		return loginService.updateLoginByUid(loginModel);
	}
	
	/**
	*
	*  重置密码
	*/
	@RequestMapping(value = "resetPassword", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public ResultModel<String> resetPassword(@RequestParam(value = "password") String password, HttpServletRequest request) {
		if (StringUtils.isEmpty(password)) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "重置失败", "请求参数有误");
		}
		String login = (String)request.getSession().getAttribute("login");
		if (StringUtils.isEmpty(login)) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "手机号/邮箱错误", "请求参数有误");
		}
		return loginService.resetPassword(login,password);
	}
	
	/**
	 * 查询当前用户的登录方式(一般有多种)
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "querySelfLogins", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResultModel<List<LoginModel>> querySelfLogins(HttpServletRequest request) {
		String uid = UserUtil.getUidByRequest(request);
		if (StringUtils.isEmpty(uid)) {
			return new ResultModel<List<LoginModel>>(ResultModel.ERR_PARAM, "请求参数有误", null);
		} 
		List<LoginModel> loginModels = loginService.queryLoginByUid(uid);
		if(Utils.isEmptyCollection(loginModels)) {
			return new ResultModel<List<LoginModel>>(ResultModel.NOT_FOUND_OBJECT, "查询失败,数据不存在", null);
		}
		return new ResultModel<List<LoginModel>>(ResultModel.SUCCESS, "成功获取login", loginModels);
	}
	
	/**
	 * 获取公众号平台appid
	 * @return
	 */
	@GetMapping(value = "getWxAppAppid", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResultModel<String> getWxAppAppid() {
		if(StringUtils.isEmpty(wxAppAppid)) {
			logger.warn("获取公众平台appid失败");
			return new ResultModel<String>(ResultModel.NOT_FOUND_OBJECT, "系统错误,请联系系统管理员", null);
		}
		return new ResultModel<String>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, wxAppAppid);
	}
	
	/**
	 * 获取微信开放平台appid
	 * @return
	 */
	@GetMapping(value = "getWxAppid", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResultModel<String> getWxAppid() {
		if(StringUtils.isEmpty(wxAppid)) {
			logger.warn("获取微信开放平台appid失败");
			return new ResultModel<String>(ResultModel.NOT_FOUND_OBJECT, "系统错误,请联系系统管理员", null);
		}
		return new ResultModel<String>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, wxAppid);
	}
}